//
//  XJCTimeViewController.m
//  On The Load
//
//  Created by 许嘉成 on 15/11/6.
//  Copyright © 2015年 @@叶一闽. All rights reserved.
//

#import "XJCTimeViewController.h"
#import "AppTools.h"
#import "XJCVideoModel.h"
#import "XJCTimeTableViewCell.h"
#import "EntertainmentDetailsViewController.h"
#import <MJRefresh/MJRefresh.h>

@interface XJCTimeViewController ()<UITableViewDataSource,UITableViewDelegate,removeMyController,UIScrollViewDelegate>

@property (nonatomic, strong) UITableView *videoTV;
@property (nonatomic, strong) NSMutableArray *videoArray;
@property (nonatomic, weak) UIViewController *popController;
@property (nonatomic, assign) NSInteger currentPage;

@end

@implementation XJCTimeViewController

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    

}
- (void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.navigationController.navigationBar.translucent = YES;
    [self setAutomaticallyAdjustsScrollViewInsets:YES];
    self.videoArray = [NSMutableArray array];
    _currentPage = 0;
    [self createView];
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(getAFN)];
    _videoTV.footer = footer;
    [self getAFN];
}

- (void)getAFN{
    
    NSString *urlStr = @"";
    
    if (_currentPage != 0) {
        urlStr = [NSString stringWithFormat:@"http://baobab.wandoujia.com/api/v1/videos?start=%ld&num=10&categoryName=%%E6%%97%%85%%E8%%A1%%8C&strategy=date&vc=125&t=MjAxNTExMDkxNjE5MDQzNjUsNzM4NQ%%3D%%3D&u=8ec6699acd6fc4d031604939eac83d6c785eb50f&net=wifi&v=1.8.1&f=iphone", _currentPage];
        NSLog(@"%@", urlStr);
    }else{
        urlStr = @"http://baobab.wandoujia.com/api/v1/videos?num=10&categoryName=旅行&strategy=date&vc=125&t=MjAxNTExMDUxNzE4MTczMTMsOTE4MA%3D%3D&u=7e508a0fecf9bcb579f260fc0ed8fa4ef415a20e&net=wifi&v=1.8.1&f=iphone";
        urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    }
    
    
    [AppTools getAFN:urlStr andpara:nil andBlock:^(id result) {
        NSArray *bigArray = [result objectForKey:@"videoList"];
        
        for (NSMutableDictionary *dic in bigArray) {
            XJCVideoModel *model = [[XJCVideoModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            model.descrip = [dic objectForKey:@"description"];
            model.iD = [dic objectForKey:@"id"];
            [self.videoArray addObject:model];
        }
        
        _currentPage += 10;
        [self.videoTV reloadData];
        [self.videoTV.footer endRefreshing];
    } andfailBlock:^(id result) {
        
        NSLog(@"请求失败");
    }];
}
#pragma  mark-- EntertainmentViewController  DataSource
-(void)popMyController:(NSInteger)index andDataArray:(NSMutableArray *)array
{
    
    EntertainmentDetailsViewController *detailVC = [[EntertainmentDetailsViewController
                                                     alloc]init];
    [UIView animateWithDuration:0.1 animations:^{
        detailVC.titleLabel.alpha = 0;
        detailVC.timeLabel.alpha = 0;
        detailVC.lineView.alpha = 0;
        detailVC.descLabel.alpha = 0;
        
    }];
    
    if (index != 0) {
        CGPoint point = self.videoTV.contentOffset;
        point.y += index * 200 * SCALEH;
        self.videoTV.contentOffset = point;
    }
    
    self.videoArray = array;
    [self.videoTV reloadData];
    
    //调用showHeaderView协议方法
    [self.showDelegate showHeaderView];
}

- (void)createView{

    self.videoTV = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 64+40*SCALEH)];

    self.videoTV.tableHeaderView = view;
    self.videoTV.delegate = self;
    self.videoTV.dataSource = self;
    self.videoTV.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.videoTV];
}

#pragma mark -- UITableview data Source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.videoArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    static NSString *cellIdentifier = @"cell";
    XJCTimeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[XJCTimeTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.backgroundColor = [UIColor colorWithRed:0.686 green:1.000 blue:0.885 alpha:1.000];
    if (self.videoArray.count != 0) {
        XJCVideoModel *videoModel = [self.videoArray objectAtIndex:indexPath.row];
        cell.videoModel = videoModel;
    }
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 200*SCALEH;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    XJCTimeTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    CGRect rectInTableView = [tableView rectForRowAtIndexPath:indexPath];
    CGRect rect = [tableView convertRect:rectInTableView toView:[tableView superview]];
    
    EntertainmentDetailsViewController *detailVC = [[EntertainmentDetailsViewController alloc]init];
    detailVC.tempImage = cell.picImg.image;
    detailVC.rect = rect;
    detailVC.delegate = self;
    detailVC.dataArray = self.videoArray;
    detailVC.view.backgroundColor = [UIColor clearColor];
    detailVC.contentSet = indexPath.row;
    detailVC.type = 0;
    
    [self addChildViewController:detailVC];
    [self.view addSubview:detailVC.view];
    self.popController = detailVC;
//    NSLog(@"%@",NSStringFromCGRect(rect));
    
    //调用hideHeaderView协议方法
    [self.hideDelegate hideHeaderView];
    
}

@end
